/**
 * Created by ZHOUSH on 1/12/2016.
 */
ngctrl.directive('dbForm', ['$document', '$window', 'RecursionHelper',
    function ($document, $window, RecursionHelper) {
        return {
            restrict: "EA",
            templateUrl: "/template/form",
            replace: true,
            scope: {
                form: '=dbBind'
            },
            compile: function (element) {
                return RecursionHelper.compile(element, {
                    pre: function (scope, element, attrs) {
                        var form = scope.form;
                        form.fields = form.fields.map(function (field) {
                            var ret = field;
                            if (typeof field == 'string') {
                                if (field == '-') {
                                    ret.split = true;
                                } else {
                                    var fieldSpec = /(\[(.*?)\])?(.*)/ig.exec(field.split('=>')[0]),
                                        inline = true,
                                        multi = false,
                                        noLabel = false;
                                    var disp = fieldSpec[3], props = (fieldSpec[2] || '').split(',');
                                    if (props.indexOf('block') >= 0) {
                                        inline = false;
                                    }
                                    if (props.indexOf('multi') >= 0) {
                                        multi = true;
                                    }
                                    if (props.indexOf('noLabel') >= 0) {
                                        noLabel = true;
                                    }
                                    var val = props.filter(function (prop) {
                                        return prop[0] == '@';
                                    })[0];
                                    val = val ? val.substring(1, val.length) : val;
                                    var params = field.split('=>')[1] || 'text';
                                    var bind = function (attr) {
                                        if (attr[0] == '@') {
                                            return form[attr.substring(1, attr.length)];
                                        } else {
                                            return attr;
                                        }
                                    };
                                    ret = {
                                        display: disp,
                                        value: val,
                                        prop: val || disp,
                                        inputs: params.split('|').map(function (attr) {
                                            attr = attr.split(',');
                                            var type = attr[0];
                                            var ret = {
                                                type: type,

                                            };
                                            if (attr[1]) ret.placeholder = bind(attr[1]);
                                            if (attr[2]) {
                                                if (type == 'radio') {
                                                    ret.group = bind(attr[2]);
                                                } else if (type == 'listview') {
                                                    ret.box = true;
                                                }
                                            }
                                            if (attr[3]) {
                                                if (type == 'radio') {
                                                    ret.value = bind(attr[3]);
                                                } else if (type == 'listview') {
                                                    ret.minHeight = bind(attr[3]);
                                                }
                                            }
                                            return ret
                                        }),
                                        inline: inline,
                                        multi: multi,
                                        noLabel: noLabel,
                                        buttons: props.indexOf('buttons') >= 0,
                                        buttonsFloat: props.indexOf('left') >= 0 ? 'left' : 'right'
                                    }
                                }
                            }
                            var inputs = ret.inputs || [];
                            if (ret.input) {
                                inputs = [ret.input].concat(inputs);
                            }
                            if (inputs.length > 0) {
                                var l = inputs.length, t = 0;
                                inputs.forEach(function (input, idx) {
                                    if (!input.width && (idx != l - 1)) {
                                        input.width = +((12 / l).toFixed(0));
                                    } else if (!input.width) {
                                        input.width = 12 - t;
                                    }
                                    t += (+input.width);
                                    if (ret.buttons) {
                                        input.class = ret.buttonsFloat + '-buttons';
                                    } else {
                                        input.class = 'col-xs-' + input.width;
                                    }
                                });
                                if (ret.buttons) ret.noLabel = true;
                                if (ret.buttons && ret.buttonsFloat == 'right') {
                                    inputs.reverse();
                                }
                            }
                            ret.inputs = inputs;
                            return ret;
                        });
                        var items = [], maxCol = 0;
                        form.fields.forEach(function (field) {
                            if (field.inline === true) {
                                items.push({
                                    inline: true,
                                    noLabel: field.noLabel,
                                    field: field
                                });
                                if (field.inputs.length + 1 > maxCol) {
                                    maxCol = field.inputs.length + 1;
                                }
                            } else {
                                items.push({
                                    inline: false,
                                    noLabel: field.noLabel,
                                    field: field,
                                    split: field.split,
                                    label: true
                                });
                                if (!field.split) {
                                    field.inputs.forEach(function (input, idx) {
                                        items.push({
                                            idx: idx,
                                            inline: false,
                                            field: field,
                                            input: input,
                                            label: false,
                                            common: input.type != 'select' && input.type != 'textarea'
                                        })
                                    });
                                }
                            }
                        });
                        var props = form.fields.map(function (field) {
                            return field.prop;
                        });
                        props.forEach(function (prop) {
                            if (!form.data[prop]) {
                                form.data[prop] = [];
                            }
                        });
                        form.layout = {
                            items: items,
                            colspan: maxCol
                        }
                    },
                    post: function (scope, element, attrs) {
                        console.log(scope);
                    }
                });
            }
        };
    }
]);